import {Request,Login} from "../../utils/request.js";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: getApp().globalData.statusBarHeight,
    open:1, //切换 基本心动 完美心动
    height:'1vh',
    tab: 1, //默认第一个是心动页面
    qiheOpen:1, //切换 基本契合 完美契合
    gaojistatus: 0,//高级信息开关
    basicBasicpage: 10,//基本心动分页码
    basicWantpage: 10,//基本心动分页码
    endpage: 10,//共需要查询数目
    height: (app.globalData.windowHeight - app.globalData.statusBarHeight*2 - 70) +'px',  //默认窗口高度
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    function formatDate(now) { 
      var year=now.getFullYear();  //取得4位数的年份
      var month=now.getMonth()+1;  //取得日期中的月份，其中0表示1月，11表示12月
      var date=now.getDate();      //返回日期月份中的天数（1到31）
      var hour=now.getHours();     //返回日期中的小时数（0到23）
      var minute=now.getMinutes(); //返回日期中的分钟数（0到59）
      var second=now.getSeconds(); //返回日期中的秒数（0到59）
      return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second; 
      } 
      var g=1616757459898; //定义一个时间戳变量
      var d=new Date(g);   //创建一个指定的日期对象
      console.log(d);
      console.log(formatDate(d));


    var date = new Date('2021-04-06 16:19:00');
    // 有三种方式获取
    var time1 = date.getTime();
    var time2 = date.valueOf();
    var time3 = Date.parse(date);
    console.log(time1);//1398250549123
    console.log(time2);//1398250549123
    console.log(time3);//1398250549000

    // wx.startPullDownRefresh();
    this.setData({
      navbarBarHeight: 102 + this.data.statusBarHeight,
      navbarBarMarginTop: 68
    });
  },
  /**
   * 
   * @param {切换 心动 | 契合 | 搜索} param 
   */
  _TitleQieTap:function(param){
    if(param.currentTarget.id == 1){
      this.data.tab = 1;
      clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
      this._MonitorPage('#userinfo-box-one');
    }else if(param.currentTarget.id == 2){
      this.data.tab = 2;
      clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
      //开启定时器 铺获
      this._MonitorPage('#userinfo-box-height-one');
    }else if(param.currentTarget.id == 3){
      this.data.tab = 3;
      clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
    }
    
    this.setData({
      tab: this.data.tab,
      _current: 0,
      open:1
    });
  },
  /**
   * 
   * @param {切换基本契合 | 完美契合} param 
   */
  qihetap:function(param){
    if(param.currentTarget.id == 1){
      this.data.qiheOpen = 1;
    }else if(param.currentTarget.id == 2){
      this.data.qiheOpen = 2;
    }
    this.setData({
      qiheOpen: this.data.qiheOpen,
    });
  },
  /**
   * 
   * @param {切换基本心动 | 完美心动} param 
   */
  BasicQietap:function(param){
    console.log(param);
    if(param.currentTarget.id == 1){

      if(this.data.basicCode != 2){
       //调用之前关闭
       clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
       //开启定时器 铺获
       this._MonitorPage('#userinfo-box-two');
      }
    }else if(param.currentTarget.id == 2){

      if(this.data.perfectCode != 2){
        //调用之前关闭
        clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
        //开启定时器 铺获
        this._MonitorPage('#userinfo-box-two');
      }
    }else if(param.currentTarget.id == 3){
      this.data.open = 3;
    }
    
    this.setData({
      _current: (param.currentTarget.id - 1),
    });
  },

  /**
   * 
   * @param {切换基本契合 | 完美契合} param 
   */
  qhtap:function(param){
    console.log(param);
    if(param.currentTarget.id == 1){

      if(this.data.basicCode != 2){
       //调用之前关闭
       clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
       //开启定时器 铺获
       this._MonitorPage('#userinfo-box-height-one');
      }
    }else if(param.currentTarget.id == 2){

      if(this.data.perfectCode != 2){
        //调用之前关闭
        clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
        //开启定时器 铺获
        this._MonitorPage('#userinfo-box-two');
      }
    }else if(param.currentTarget.id == 3){
      this.data.open = 3;
    }
    
    this.setData({
      // open: this.data.open,
      _current: (param.currentTarget.id - 1),
    });
  },

  /**
   * 
   * 契合切换 
   */

  qietap:function(param){
    if(param.currentTarget.id == 1){
      this.data.open = 1;
      if(this.data.basicCode != 2){
        this.rich = '#userinfo-box-one';
        setTimeout(function(){
          wx.createSelectorQuery().select(this.rich).boundingClientRect(function(res){
            var height = res.height;
            this.setData({
              height: height + 50 + 'px',
              // opens: this.data.opens,
            });
          }.bind(this)).exec();
        }.bind(this), 1);
      }
    }else if(param.currentTarget.id == 2){
      this.data.open = 2;
      if(this.data.perfectCode != 2){
        this.rich = '#userinfo-box-two';
        this.richHeight = '80vh';
        setTimeout(function(){
          wx.createSelectorQuery().select(this.rich).boundingClientRect(function(res){
            var height = res.height;
            this.setData({
              height: this.richHeight,
              // opens: this.data.opens,
            });
          }.bind(this)).exec();
        }.bind(this), 1);
      }
    }else if(param.currentTarget.id == 3){
      this.data.open = 3;
    }
    
    this.setData({
      open: this.data.open,
      _current: (this.data.open - 1),
    });
  },

  /**
   * 跳转用户详情页面
   */
  gotoFriendInfo:function(evevt){
    if(evevt.currentTarget.dataset.userid == '' || evevt.currentTarget.dataset.userid == undefined){
      new Request().showToast('userId为空');
      return;
    }
    //游客不可查看用户详情信息
    // if(app.globalData.loginCode == -1){
    //   //用户未注册 提示跳转注册页面
    //   new Login().LoginDown();
    //   return;
    // }
    wx.navigateTo({
      url: '/pages/zhaon_friendinfo/zhaon_friendinfo?userid=' + evevt.currentTarget.dataset.userid,
    });
  },
  /**
   * 
   * @param {*跳转VIP页面} res 
   */
  _gotovip:function(res){
    console.log(res)
    wx.navigateTo({
      url: '/pages/zhaon_ziown/zhaon_ziown_vip/zhaon_ziown_vip',
    });
  },

  /**
   * 心动列表 swiper滑块切换
   */
  _bindchange:function(res){
    this.setData({
      open: (res.detail.current + 1),
      _current:res.detail.current,
    });
    if(res.detail.current == 0){
      this.data.open = 1;
      if(this.data.basicCode != 2){
        //调用之前关闭
        clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
        //开启定时器 铺获
        this._MonitorPage('#userinfo-box-one');
      }
    }else if(res.detail.current == 1){
      console.log(res);
      clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
      this.data.open = 2;
      //开启定时器 铺获
      this._MonitorPage('#userinfo-box-two');
    }
  },
  /**
   * 契合列表 swiper滑块切换
   */
  _bindchangeHeight:function(res){
    this.setData({
      open: (res.detail.current + 1),
      _current:res.detail.current,
    });
    if(res.detail.current == 0){
      this.data.open = 1;
      if(this.data.basicCode != 2){
        //调用之前关闭
        clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
        //开启定时器 铺获
        this._MonitorPage('#userinfo-box-height-one');
      }
    }else if(res.detail.current == 1){
      console.log(res);
      clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
      this.data.open = 2;
      //开启定时器 铺获
      this._MonitorPage('#userinfo-box-height-two');
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //调用之前关闭
    clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
    //开启定时器 铺获
    this._MonitorPage('#userinfo-box-one');
  },
  /**
   * 
   * @param {
   *监听基本心动列表 页面长度
   *page : 监听页面id元素
   *LogSetInterval: 初始化巡航铺获
   *LogSetIntNum: 记录巡航铺获15次退出
   *}
   */
  _MonitorPage:function(page = '',LogSetIntNum = 15,LogSetInterval = new Array()){
    this.SetIntNumberUserInfo_One =  setInterval(function(param){ //巡航铺获
      wx.createSelectorQuery().select(page).boundingClientRect(function(res){
        if(res == null){
          clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
          return;
        }
        let heightView = res.height + 300;
        if(app.globalData.windowHeight >= heightView){
          this.setData({
            height: (app.globalData.windowHeight - app.globalData.statusBarHeight*2 - 70) +'px',
          });
        }else{
          this.setData({
            height: (res.height + 120)+'px',
          });
        }
        LogSetInterval.push(1);
        if(LogSetInterval.length == LogSetIntNum){
          clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
        }
      }.bind(this)).exec();
    }.bind(this),98);
  },
   /**
   * 获取搜索输入框对象id
   */
  _bindinput:function(param){
    this.searchid = param.detail.value;
  },
  /**
   * 确定搜索内容
   */
  _bindtap:function(){
    if(app.globalData.loginCode == -1){
      //用户未注册 提示跳转注册页面
      new Login().LoginDown();
      return;
    }
    if(this.searchid == undefined || this.searchid == null || this.searchid == '' ){
      new Request().showToast('请输入对象UID','error');
      return;
    }

    let arr = this.searchid.split('');
    if(arr[0] == 'u'){
      new Request().showToast('区分大写UID','error');
      return;
    }
    if(arr[0] != 'U'){
      new Request().showToast('输入有误UID','error');
      return;
    }
    this.searchid = this.searchid.slice(1);
    new Request('search/Search_Friend_Id/SearchId').request(
      () => {
        return {
          openid: app.globalData.openid,
          userid: app.globalData.userid,
          search_id: this.searchid,//对象id
        }
      },
      (res) => {
        if(res.data.code == 0){
          new Request().showToast(res.data.msg);
          return;
        }
        let url =   wx.getStorageSync("config");
        let reg = new RegExp("女");
        res.data.data.map(function(e){
          e.SQLhome     = e.city;
          e.name        = e.nichen;
          e.SQLposition = e.job;
          e.SQLheight   = e.hei;
          e.userimage   = url.service.host + e.avatar;
          e.SQLsex      = reg.test(e.gender);
          if(e.text == '' || e.text == undefined){
            e.text = 'TA还没有独白哟………';
          }
        });
        console.log(res.data.data);
        this.setData({
          userinfo: res.data.data,
          userStatus: true,
        });
    });

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //判断是否游客
    if(app.globalData.loginCode == -1){
      var basicUrl = 'basic/match/Get_Rand_List/RandList';
    }else{
      var basicUrl = 'basic/match/Get_General/GetList';
    }
    /**
    * 获取用户基本心动列表
    */
      new Request(basicUrl).request(
      () => {
        return {
          openid: app.globalData.openid,
          userid: app.globalData.userid,
        }
      },
      (res) => {
        if(!res.data.data){
          if(app.globalData.showStatus){
            new Request().showToast('暂无好友心动');
            //调用监听高度
            this.setData({basicCode: res.data.code});
            app.globalData.showStatus = false;
          }
          return;
        }
        let reg = new RegExp("女");
        res.data.data.map(function(e){
          e.SQLhome     = e.city;
          e.name        = e.nichen;
          e.SQLposition = e.job;
          e.SQLheight   = e.hei;
          e.userimage   = e.avatar;
          e.SQLsex      = reg.test(e.gender);
          if(e.text == '' || e.text == undefined){
            e.text = 'TA还没有独白哟………';
          }
        });
        this.setData({
          basicList: res.data.data,
          basicCode: res.data.code,   //基本心动  等于-2没有 匹配合适的对象
        });
      });
      if(app.globalData.loginCode == -1){
        new Request().showToast('当前是游客身份');
        this.setData({vip: -2});
        return;
      }
      //完美心动 需要VIP2会员
      if(app.globalData.userVip < 2 || app.globalData.userVip == undefined){
        //还未开通会员  
        this.setData({vip: -2});
        return;
      }
      /**
      * 获取用户完美心动列表
      */
      new Request('basic/match/Get_Perfect/GetList').request(
      () => {
        return {
          openid: app.globalData.openid,
          userid: app.globalData.userid,
          vip:app.globalData.userVip
        }
      },
      (res) => {
        if(app.globalData.userVip < 2 || app.globalData.userVip == undefined){
          //还未开通会员  
          this.setData({vip: -2});
          return;
        }
        if(!res.data.data){
          // new Request().showToast(res.data.msg);
          //没有用户匹配
          this.setData({perfectCode: -2});
          return;
        }
        let reg = new RegExp("女");
        res.data.data.map(function(e){
          e.SQLhome     = e.city;
          e.name        = e.nichen;
          e.SQLposition = e.job;
          e.SQLheight   = e.hei;
          e.userimage   = e.avatar;
          e.SQLsex      = reg.test(e.gender);
          if(e.text == '' || e.text == undefined){
            e.text = 'TA还没有独白哟………';
          }
        });
        this.setData({
          PerfectList: res.data.data,
        });
      });
      //基本契合 需要VIP3会员
      if(app.globalData.userVip <= 2 || app.globalData.userVip == undefined){
        //还未开通会员  
        this.setData({vip: -3,});
        return;
      }
    /**
    * 获取用户基本契合列表
    */
    new Request('senior/match/Get_General/GetList').request(
      () => {
        return {
          openid: app.globalData.openid,
          userid: app.globalData.userid,
        }
      },
      (res) => {
        if(app.globalData.userVip <= 2 || app.globalData.userVip == undefined){
          //还未开通会员  
          this.setData({vip: -3,});
          return;
        }
        if(!res.data.data){
          // new Request().showToast('暂无好友契合');
          //调用监听高度
          this.setData({seniorCode: res.data.code});
          return;
        }
        let reg = new RegExp("女");
        res.data.data.map(function(e){
          e.SQLhome     = e.city;
          e.name        = e.nichen;
          e.SQLposition = e.job;
          e.SQLheight   = e.hei;
          e.userimage   = e.avatar;
          e.SQLsex      = reg.test(e.gender);
          if(e.text == '' || e.text == undefined){
            e.text = 'TA还没有独白哟………';
          }
        });
        this.setData({
          seniorList: res.data.data,
          seniorCode: res.data.code,  
        });
      });
      //完美契合 需要VIP4会员
      if(app.globalData.userVip <= 3 || app.globalData.userVip == undefined){
        //还未开通会员  
        this.setData({vip: -4,});
        return;
      }
      /**
      * 获取用户完美契合列表
      */
      new Request('senior/match/Get_Perfect/GetList').request(
        () => {
          return {
            openid: app.globalData.openid,
            userid: app.globalData.userid,
          }
        },
        (res) => {
          if(app.globalData.userVip <= 3 || app.globalData.userVip == undefined){
            //还未开通会员  
            this.setData({vip: -4,});
            return;
          }
          if(!res.data.data){
            //没有用户匹配
            this.setData({perfectSeniorCode: -2});
            return;
          }
          let reg = new RegExp("女");
          res.data.data.map(function(e){
            e.SQLhome     = e.city;
            e.name        = e.nichen;
            e.SQLposition = e.job;
            e.SQLheight   = e.hei;
            e.userimage   = e.avatar;
            e.SQLsex      = reg.test(e.gender);
            if(e.text == '' || e.text == undefined){
              e.text = 'TA还没有独白哟………';
            }
          });
          this.setData({
            seniorPerfectList: res.data.data,
            perfectSeniorCode: res.data.code,   //完美  等于2没有 匹配合适的对象
          });
        });
        return;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    new Request().showToast('正在加载中...', 'loading');
    //tab: 1是基本心动列表 open：1是
    if(this.data.tab == 1 && this.data.open == 1){
      console.log('加载基本心动列表')
      new Request('basic/match/Get_General/GetList').request(
        () => {
          return {
            openid: app.globalData.openid,
            startpage: this.data.basicBasicpage+=10,//开始查询页码
            endpage: this.data.endpage,//需要查询数目
          }
        },
        (res) => {
          if(!res.data.data){
            new Request().showToast(res.data.msg);
            this.data.basicBasicpage-=10;
            return;
          }
          let reg = new RegExp("女");
          res.data.data.map(function(e){
            e.SQLhome     = e.city;
            e.name        = e.nichen;
            e.SQLposition = e.job;
            e.SQLheight   = e.hei;
            e.userimage   = e.avatar;
            e.SQLsex      = reg.test(e.gender);
            if(e.text == '' || e.text == undefined){
              e.text = 'TA还没有独白哟………';
            }
          });
          //合并数组
          let newsBasic = this.data.basicList.concat(res.data.data);
          this.setData({
            basicList: newsBasic
          });
          //调用之前关闭
          clearInterval(this.SetIntNumberUserInfo_One);  //关闭 铺获
          //开启定时器 铺获
          this._MonitorPage('#userinfo-box-one');
        });
    }else if(this.data.tab == 1 && this.data.open == 2){
      console.log('加载完美心动列表');
      //还未开通会员
      if(app.globalData.userVip <= 1){
        //还未开通会员
        new Request().showToast('未开通会员', 'none');
        return;
      }
      /**
      * 获取用户完美心动列表
      */
     new Request('basic/match/Get_Perfect/GetList').request(
      () => {
        return {
          openid: app.globalData.openid,
          startpage: this.data.basicWantpage+=10,
          endpage: this.data.endpage,
        }
      },
      (res) => {
       if(!res.data.data){
        new Request().showToast(res.data.msg);
        this.data.basicWantpage-=10;
        return;
      }
      let reg = new RegExp("女");
        res.data.data.map(function(e){
          e.SQLhome     = e.city;
          e.name        = e.nichen;
          e.SQLposition = e.job;
          e.SQLheight   = e.hei;
          e.userimage   = e.avatar;
          e.SQLsex      = reg.test(e.gender);
          if(e.text == '' || e.text == undefined){
            e.text = 'TA还没有独白哟………';
          }
        });
       let newsPerfectList = this.data.PerfectList.concat(res.data.data);
        this.setData({
          PerfectList: newsPerfectList
        });
      });
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})